from sqlmodel import Session
from ..core.database import engine
from ..models.permission import User, Role, Permission, Menu
from ..crud.permission import create_user, create_role, create_permission, create_menu
from ..schemas.permission import UserCreate, RoleCreate, PermissionCreate, MenuCreate

def init_system_data():
    """初始化系统数据"""
    with Session(engine) as session:
        # 创建默认角色
        admin_role = create_role(session, RoleCreate(
            name="管理员",
            code="admin",
            description="系统管理员角色"
        ))

        user_role = create_role(session, RoleCreate(
            name="普通用户",
            code="user",
            description="普通用户角色"
        ))

        # 创建默认权限
        permissions_data = [
            {
                "name": "用户管理",
                "code": "user:manage",
                "description": "用户管理权限",
                "resource_type": "menu"
            },
            {
                "name": "角色管理",
                "code": "role:manage",
                "description": "角色管理权限",
                "resource_type": "menu"
            },
            {
                "name": "菜单管理",
                "code": "menu:manage",
                "description": "菜单管理权限",
                "resource_type": "menu"
            },
            {
                "name": "新增用户按钮",
                "code": "user:create",
                "description": "新增用户按钮权限",
                "resource_type": "button"
            },
            {
                "name": "编辑用户按钮",
                "code": "user:edit",
                "description": "编辑用户按钮权限",
                "resource_type": "button"
            },
            {
                "name": "删除用户按钮",
                "code": "user:delete",
                "description": "删除用户按钮权限",
                "resource_type": "button"
            }
        ]

        for perm_data in permissions_data:
            create_permission(session, PermissionCreate(**perm_data))

        # 创建默认菜单
        menus_data = [
            {
                "name": "系统管理",
                "path": "/system",
                "component": "Layout",
                "icon": "setting",
                "order": 1
            },
            # {
            #     "name": "用户管理",
            #     "path": "/system/users",
            #     "component": "Users",
            #     "icon": "user",
            #     "parent_id": 1,
            #     "order": 1
            # },
            # {
            #     "name": "角色管理",
            #     "path": "/system/roles",
            #     "component": "Roles",
            #     "icon": "role",
            #     "parent_id": 1,
            #     "order": 2
            # },
            # {
            #     "name": "菜单管理",
            #     "path": "/system/menus",
            #     "component": "Menus",
            #     "icon": "menu",
            #     "parent_id": 1,
            #     "order": 3
            # },
            {
                "name": "仪表板",
                "path": "/dashboard",
                "component": "Dashboard",
                "icon": "dashboard",
                "order": 0
            }
        ]

        menu_instances = []
        for menu_data in menus_data:
            menu_instance = create_menu(session, MenuCreate(**menu_data))
            menu_instances.append(menu_instance)

        # 创建默认管理员用户
        admin_user = create_user(session, UserCreate(
            username="admin",
            email="admin@example.com",
            password="admin123",
            full_name="系统管理员",
            is_superuser=True
        ))

        print("系统初始化数据创建完成")

if __name__ == "__main__":
    init_system_data()
